package com.joseph.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.joseph.annotation.RequireRole;

/**
 * @description 
 * @author Joseph_Mok
 * @date 2016年3月29日下午2:38:34
 */
public class RoleProxy implements InvocationHandler {

	private Object target;
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Annotation annotation = method.getAnnotation(RequireRole.class);
		if (annotation != null) {
			System.out.println("need role");
			return null;
		}
		System.out.println(proxy.getClass().getName());
		method.invoke(target, args);
		return null;
	}
	
	public Object returnProxy(Object target) {
		this.target = target;
		return Proxy.newProxyInstance(this.getClass().getClassLoader(),
				target.getClass().getInterfaces(), this);
	}
}
